import requests

url = "https://api.gainfollow.me/api/iap/items?isClearCache=true&productId=com.bbqstar.besttag&lang=zh&session=MTg3OTgwNzc7dmljdG9yYWZyYTsxNTUzODU0MDM2OTQxOzA&gsid=47ef18558533366530e5d9be5a9729ac639431bb"


end_file = "bean.txt"


method_first_line = "List<KernelBuyItemBean> list = new ArrayList<>();"
method_data_first_line = "list.add(new KernelBuyItemBean("
method_end_line = "return list;"


def write_to_file(text, path=end_file):
    with open(path, 'a', encoding='utf-8') as f:
        f.writelines(text + '\n')


def write_to_file_and_clear(text, path=end_file):
    with open(path, 'w', encoding='utf-8') as f:
        f.writelines(text + '\n')


def write_date(list, variety_name, view_holder_name):
    if len(list) == 0:
        write_to_file("return null;")
        return
    write_to_file(method_first_line)
    for item in list:
        write_to_file(method_data_first_line)
        write_to_file('        CoreBuyItemVarietyEnum.' + variety_name + '.getItemType(),')
        write_to_file('       "' + item['itemName'] + '",')
        write_to_file('       ' + str(item['itemCount']) + ',')
        write_to_file('       "",')
        iapId = item['iapId']
        if iapId == None or iapId == 'None' or iapId == 'null':
            iapId = ''
        write_to_file('       "' + iapId + '",')
        write_to_file('       ' + str(item['costAvg']) + ',')
        write_to_file('       ' + str(item['costTotal']) + ',')
        if view_holder_name == "":
            date_end = 'BaseFlag.DefaultData.NONE'
        else:
            date_end = 'CoreViewHolderFactory.' + view_holder_name
        write_to_file('       ' + date_end + '));')
    write_to_file(method_end_line)


if __name__ == '__main__':
    json = requests.get(url).json()
    print(json)
    all = json['items']
    like_coins = []
    like_money = []
    like_vip_weekly = []
    like_vip_monthly = []
    follow_coins = []
    follow_money = []
    follow_vip_weekly = []
    follow_golden_coins = []
    follow_golden_money = []
    coins_money = []
    coins_vip = []
    for item in all:

        itemType = item['itemType']
        if itemType == 'like_coins':
            like_coins.append(item)
        elif itemType == 'like_money':
            like_money.append(item)
        elif itemType == 'like_vip_weekly':
            like_vip_weekly.append(item)
        elif itemType == 'like_vip_monthly':
            like_vip_monthly.append(item)
        elif itemType == 'follow_coins':
            follow_coins.append(item)
        elif itemType == 'follow_money':
            follow_money.append(item)
        elif itemType == 'follow_vip_weekly':
            follow_vip_weekly.append(item)
        elif itemType == 'follow_golden_coins':
            follow_golden_coins.append(item)
        elif itemType == 'follow_golden_money':
            follow_golden_money.append(item)
        elif itemType == 'coins_money':
            coins_money.append(item)
        elif itemType == 'coins_vip':
            coins_vip.append(item)

    write_to_file_and_clear("@Override\npublic List<KernelBuyItemBean> getDefaultFollowBuyItemByMoney() {")
    write_date(follow_money, "FOLLOW_MONEY", "BUY_FOLLOW_BY_MONEY")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultGoldenFollowBuyItemByMoney() {")
    write_date(follow_golden_money, "FOLLOW_GOLDEN_MONEY", "BUY_FOLLOW_BY_MONEY")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultFollowBuyItemByCoin() {")
    write_date(follow_coins, "FOLLOW_COINS", "BUY_FOLLOW_BY_COIN")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultGoldenFollowBuyItemByCoin() {")
    write_date(follow_golden_coins, "FOLLOW_GOLDEN_COINS", "BUY_FOLLOW_BY_COIN")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultLikeBuyItemByMoney() {")
    write_date(like_money, "LIKE_MONEY", "BUY_LIKE_BY_MONEY")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultLikeBuyItemByCoin() {")
    write_date(like_coins, "LIKE_COINS", "BUY_LIKE_BY_COIN")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultLikeVipWeeklyList() {")
    write_date(like_vip_weekly, "LIKE_VIP_WEEKLY", "")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultLikeVipMonthlyList() {")
    write_date(like_vip_monthly, "LIKE_VIP_MONTHLY", "")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultFollowVipWeeklyList() {")
    write_date(follow_vip_weekly, "FOLLOW_VIP_WEEKLY", "")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultCoinsVipWeeklyList() {")
    write_date(coins_vip, "COINS_VIP", "")
    write_to_file("}\n")
    write_to_file("@Override")
    write_to_file("public List<KernelBuyItemBean> getDefaultCoinByMoneyList() {")
    write_date(coins_money, "COINS_MONEY", "BUY_COINS_BY_MONEY")
    write_to_file("}\n")

